<div id="tb<?php echo $uniqid;?>">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:void(0);" onclick="agentqualifygoodsReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
            <td><span style="line-height:30px;"></span>
                <input id="agentqualifygoodskeywords" style="width:120px" placeholder="按ID、商品名搜索" type="text"
                                   class="easyui-input textbox"></td>
            <td><a href="javascript:void(0);" onclick="agentqualifygoodsSearch();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span class="l-btn-text">查询</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="con"  style="-moz-user-select:none;">
    <table id="agentqualifygoods"></table>
</div>
<script>
    var cfgGoods = JSON.parse('<?php echo $cfgGoods;?>');
    var agentqualifygoodsReload = function () {
        $("#agentqualifygoods").datagrid('reload');
    };
    var agentqualifygoodsSearch = function (id) {
        var height = 520 - 96;//表格高度
        var width = 840 - 2;
        $("#agentqualifygoods").datagrid({
            width: width,
            height: height,//高度
            singleSelect: false,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: false,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 15,
            pageList: [15, 30, 40, 50],
            method: 'post',
            sortName: 'weigh',
            sortOrder: 'desc',
            url: '/admin/agent/goodsadd',
            queryParams: {
                keywords: $('#agentqualifygoodskeywords').val(),
            },
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: false,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: true,
            checkOnSelect: true,
            idField: 'id',
            toolbar: '#tb<?php echo $uniqid;?>',
            rowStyler: function (index, row) {
                if (row.status == '失效') {
                    return 'color:#F00;font-style:italic;';
                }
            },
            columns: [[
                {field: 'id', checkbox: true, width: '5%'},
                {
                    field: 'ids',
                    title: 'ID',
                    width: '10%',
                    formatter: function (value, rowData, rowIndex) {
                        return rowData.id;
                    }
                },
                {
                    field: 'title', title: '名称', width: '50%', formatter: function (value, rowData, rowIndex) {
                        return '<div style="display: flex;align-items: center;">' +
                            '<img src="' + rowData.image + '?imageView2/1/w/60/h/60" style="height:30px;width:30px;border-radius: 5px;border:#dfdfdf 1px solid;">'+
                            '<div>' + value + "<br>" + (rowData.is_sku ? '（多规格）' : '') + '</div></div>';
                    }
                },
                {field: 'price', title: '价格', width: '13%', align:'center', sortable: true},
                {field: 'stock', title: '库存', width: '13%', align:'center', sortable: true},
            ]],
            onCheck(rowIndex,rowData) {
                if(cfgGoods.indexOf(rowData.id.toString())===-1) {
                    cfgGoods.push(rowData.id.toString());
                }
            },
            onCheckAll(rows){
                rows.forEach((it)=>{
                    if(cfgGoods.indexOf(it.id.toString())===-1) {
                        cfgGoods.push(it.id.toString());
                    }
                });
            },
            onUncheck(rowIndex,rowData) {
                let idx = cfgGoods.indexOf(rowData.id.toString());
                if(idx>-1) {
                    cfgGoods.splice(idx, 1);
                }
            },
            onUncheckAll(rows){
                rows.forEach((it)=>{
                    if(cfgGoods.indexOf(it.id.toString())>-1) {
                        cfgGoods.splice(cfgGoods.indexOf(it.id.toString()), 1);
                    }
                });
            },
            onLoadSuccess: function (data) {
                $.each(data.rows,  (i, val)=> {
                    if(cfgGoods.indexOf(val.id.toString())>-1){
                        $('#agentqualifygoods').datagrid('checkRow', i);
                    }
                })
            }
        });
    };
    agentqualifygoodsSearch();
    onDatagridResize("#agentqualifygoods");

    $('#agentconfigsubmit').click(function(data){
        $.post('/admin/agent/goodsflush', {goods:cfgGoods}, function(data){
            if(data.ret===0){
                agentconfigVm.params.become_agent.value = cfgGoods;
                agentconfigVm.init();
                $.messager.show({
                    title:'提示',
                    msg:data.msg,
                    timeout:3000,
                    showType:'slide'
                });
                $("#agentconfigdlg").dialog('close');
            }else{
                $.messager.alert('提示',data.msg,'warning');
            }
        });
    })
</script>
